﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataAccessLibrary
{
    public class Utility
    {
        public string CreateAccount(string name)
        {
            name = ConvertToUnsign(name);
            var temp = name.Split(' ');
            var lastidx = temp.Count() - 1;
            string emId = temp[lastidx];
            string lastemId = "";
            for (int i = 0; i < lastidx; i++)
            {
                if (temp[i].Trim() != "")
                {
                    lastemId = lastemId + temp[i];
                }
            }
            if (lastemId.Trim() != "")
            {
                emId = emId + "." + lastemId;
            }
            return emId;
        }


        public string ConvertToUnsign(string str)
        {
            string[] pattern = { "(áàảãạăắằẳẵặâấầẩẫậ)", "đ", "(éèẻẽẹêếềểễệ)", "(íìỉĩị)", "(óòỏõọôốồổỗộơớờởỡợ)", "(úùủũụưứừửữự)", "(ýỳỷỹỵ)" };
            char[] map = { 'a', 'd', 'e', 'i', 'o', 'u', 'i' };
            char[] strChar = str.ToLower().ToCharArray();

            for (var i = 0; i < pattern.Count(); i++)
            {
                for (var j = 0; j < strChar.Count(); j++)
                {
                    if (pattern[i].IndexOf(strChar[j]) > -1)
                    {
                        strChar[j] = map[i];
                    }
                }
            }

            return new string(strChar);
        }

        public string CreateRandomPassword(int passwordLength)
        {
            const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            var chars = new char[passwordLength];
            var rd = new Random();

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
            }

            return new string(chars);
        }
    }
}
